// option types are declare with ?Type
// V combines Option and Result into one type
// add a ? to the return type and return an error when something goes wrong
// `return none` is a more efficient equivalent of `return error('')`
// V does not handle exceptions with `throw/try/catch` blocks
// `err` is defined inside an `or` block and is set to the string message passed to the error() function

struct User {
	id   int
	name string
}

struct Repo {
	users []User
}

fn (r Repo) find_user_by_id(id int) ?User {
	for user in r.users {
		if user.id == id {
			// V automatically wraps this into an option type
			return user
		}
	}
	return error('User $id not found')
}

fn main() {
	repo := Repo{
		users: [User{1, 'Andrew'}, User{2, 'Bob'}, User{10, 'Charles'}]
	}
	
	// Option types must be handled by `or` blocks
	user := repo.find_user_by_id(10) or {
		println(err)
		return
	}
	println(user.id)   // "10"
	println(user.name) // "Charles"
}
